package com.wym.lottery.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CheckData {
	private static int error_code = -1;
	private static final int MONEY_TYPE = 0;
	private static final int NUMBER_TYPE = 1;

	public static boolean checkMoney(String money) {
		if (null == money)
			return false;
		return checkDataByFullDigit(money, MONEY_TYPE);
	}

	// 如果在转化的过程中出异常，就会返回一个空的List
	public static List<String> checkMoreNum(String str) {
		List<String> list = new ArrayList<String>();
		boolean flag = true;
		// 如果在转化的过程中出异常，就会返回一个空的List
		try {
			String[] arr = str.split("#");
			
			for (String string : arr) 
				flag = flag && string.matches("[0-9x]+");
			
			if (flag == true) {
				char[] ch1, ch2, ch3, ch4;
				ch1 = arr[0].toCharArray();
				ch2 = arr[1].toCharArray();
				ch3 = arr[2].toCharArray();
				ch4 = arr[3].toCharArray();

				StringBuffer sb = new StringBuffer();
				for (int i_1 = 0; i_1 < ch1.length; i_1++)
					for (int i_2 = 0; i_2 < ch2.length; i_2++)
						for (int i_3 = 0; i_3 < ch3.length; i_3++)
							for (int i_4 = 0; i_4 < ch4.length; i_4++) {
								sb.setLength(0);
								sb.append(ch1[i_1]).append(ch2[i_2]).append(ch3[i_3]).append(ch4[i_4]);
								list.add(sb.toString());
							}
			}

		} catch (Exception e) {

		}
		
		return list;
	}

	// 四现--1
	// 三现--2
	// 二现--3
	// 4码--4
	// 3码--5（111X）,6(11X1),7(1X11),8(X111)
	// 2码--9 (12xx),10(1x2x),11(x12x),12(1xx2),13(x1x2),14(xx12)
	public static int checkNum(String num) {

		if (null == num || ("").equals(num))
			return error_code;

		if (num.length() == 5)
			if (checkDataByFullDigit(num.substring(0, num.length() - 1), NUMBER_TYPE) & (num.substring(num.length() - 1).equals("c")))
				return 1;
		if (num.length() == 3 & checkDataByFullDigit(num, NUMBER_TYPE))
			return 2;
		if (num.length() == 2 & checkDataByFullDigit(num, NUMBER_TYPE))
			return 3;
		if (num.length() == 4)
			if (checkDataByFullDigit(num, 1))
				return 4;
			else
				return CheckDataByMixtureByDigitAndChar(num);

		return error_code;
	}

	// 排序号码
	public static String sort(String number) {
		char[] temp = number.toCharArray();
		Arrays.sort(temp);
		return new String(temp);
	}

	public static String resolveWinNumber(String type, String win_number) {
		int kind = Integer.parseInt(type);
		if (kind == 1)
			// 因为数据库中存的是顺序排序的数字，末尾加c
			return sort(win_number) + "c";
		else if (kind == 4)
			return win_number;
		else if (5 <= kind && kind <= 8)
			return resolve3ma(kind, win_number);
		else if (9 <= kind && kind <= 14)
			return resolve2ma(kind, win_number);

		return null;
	}

	// 如果是要2现,3现就在这个方法中查找
	public static List<String> resolve2Tor3T(String type, String win_number) {
		Set<String> set = new HashSet<String>();

		char[] chs = win_number.toCharArray();
		if ("3".equals(type))
			for (int i = 0; i < chs.length; i++)
				for (int j = i + 1; j < chs.length; j++)
					set.add(appendchar(chs[i], chs[j]));
		else if ("2".equals(type))
			for (int i = 0; i < chs.length; i++) {
				StringBuffer sb = new StringBuffer(win_number);
				set.add(sb.deleteCharAt(i).toString());
			}

		return new ArrayList<String>(set);
	}

	private static String appendchar(char a, char b) {
		StringBuffer sb = new StringBuffer();
		sb.append(a).append(b);
		return sb.toString();
	}

	private static String resolve3ma(int type, String win_number) {
		char[] temp = win_number.toCharArray();
		temp[8 - type] = 'x';
		return new String(temp);
	}

	private static String resolve2ma(int type, String win_number) {
		char[] temp = win_number.toCharArray();
		if (type == 9) {
			temp[2] = 'x';
			temp[3] = 'x';
		}
		if (type == 10) {
			temp[1] = 'x';
			temp[3] = 'x';
		}
		if (type == 11) {
			temp[0] = 'x';
			temp[3] = 'x';
		}
		if (type == 12) {
			temp[1] = 'x';
			temp[2] = 'x';
		}
		if (type == 13) {
			temp[0] = 'x';
			temp[2] = 'x';
		}
		if (type == 14) {
			temp[0] = 'x';
			temp[1] = 'x';
		}
		return new String(temp);

	}

	private static boolean checkDataByFullDigit(String str, int type) {
		Pattern p;
		switch (type) {
		case MONEY_TYPE:
			p = Pattern.compile("\\d+");
			break;

		case NUMBER_TYPE:
			p = Pattern.compile("\\d{2,4}");
			break;
		default:
			return false;
		}
		if (type == MONEY_TYPE)
			p = Pattern.compile("\\d+");
		if (type == NUMBER_TYPE)
			p = Pattern.compile("\\d{2,4}");

		Matcher m = p.matcher(str);
		boolean b = m.matches();
		return b;
	}

	private static int CheckDataByMixtureByDigitAndChar(String str) {
		if (str.matches("[0-9x]{4}") == false)
			return error_code;

		if (str.matches("\\d{3}x"))
			return 5;
		if (str.matches("\\d{2}x\\d"))
			return 6;
		if (str.matches("\\dx\\d{2}"))
			return 7;
		if (str.matches("x\\d{3}"))
			return 8;

		if (str.matches("\\d{2}x{2}"))
			return 9;
		if (str.matches("\\dx\\dx"))
			return 10;
		if (str.matches("x\\d{2}x"))
			return 11;
		if (str.matches("\\dx{2}\\d"))
			return 12;
		if (str.matches("x\\dx\\d"))
			return 13;
		if (str.matches("x{2}\\d{2}"))
			return 14;

		return error_code;

	}

}
